package com.wsd.jing.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wsd.jing.entity.BusinessLog;
import com.wsd.jing.mapper.BusinessLogMapper;
import com.wsd.jing.request.CommonRequest;
import com.wsd.jing.service.IBusinessLogService;
import com.wsd.jing.utils.SortUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * All rights Reserved, Designed By www.tom.com
 *
 * @author tom
 * @version 1.0.0
 * @projectName: jing
 * @packageName: com.wsd.jing.service.impl
 * @className: BusinessLogServiceImpl
 * @description: 业务日志服务实现类
 * @createDate 2022-11-20 07:41
 * @copyright 2022-11-20 07:41 www.tom.com Inc. All rights reserved.
 */
@Service
@RequiredArgsConstructor
@Slf4j
public class BusinessLogServiceImpl extends ServiceImpl<BusinessLogMapper, BusinessLog> implements IBusinessLogService {

    private final BusinessLogMapper businessLogMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean insertBusinessLog(BusinessLog businessLog) {
        return this.save(businessLog);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateBusinessLog(BusinessLog businessLog) {
        return this.updateById(businessLog);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteBusinessLog(Integer id) {
        return this.removeById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteBusinessLogs(String ids) {
        List<String> idList = StrUtil.splitTrim(ids, ",");
        return this.removeByIds(idList);
    }

    @Override
    public BusinessLog getBusinessLog(Integer id) {
        return this.getById(id);
    }

    @Override
    public List<BusinessLog> listBusinessLogs(CommonRequest commonRequest, BusinessLog businessLog) {
        QueryWrapper<BusinessLog> queryWrapper = new QueryWrapper<>();
        // TODO 设置查询条件

        // 处理排序字段
        SortUtil.handleListSort(commonRequest, queryWrapper);
        return this.list(queryWrapper);
    }

    @Override
    public Map<String, Object> listPageBusinessLogs(CommonRequest commonRequest, BusinessLog businessLog) {
        Map<String, Object> resultMap = new HashMap<>(2);
        Page<BusinessLog> page = new Page<>(commonRequest.getPageNum(), commonRequest.getPageSize());
        QueryWrapper<BusinessLog> queryWrapper = new QueryWrapper<>();
        // TODO 设置查询条件
        IPage<BusinessLog> result = businessLogMapper.listPageBusinessLogs(page, businessLog);
        // 处理排序字段
        resultMap.put("rows", result.getRecords());
        resultMap.put("total", result.getTotal());
        return resultMap;
    }
}
